// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Change clothes ingame
// -----------------------------------------------

private [
	"_lastbody",
	"_name",
	"_index",
	"_group",
	"_position",
	"_count",
	"_unit",
	"_newgroup",
	"_type",
	"_textbox",
	"_array"
];

if (wcoriginalclothes != typeOf player) exitWith {
	_lastbody = player;
	wcgarbage = [_lastbody] spawn WC_fnc_garbagecollector;
	_lastbody removeAllEventHandlers "Killed";
	_lastbody setPos wcinitpos;
	wcbackupbody setPos wcbackupposition;
	wcbackupbody addEventHandler ["HandleDamage", {
		(_this select 0) setDamage ((getDammage (_this select 0)) + (_this select 2));
	}];
	selectPlayer wcbackupbody;
	_lastbody setDamage 1;
	ppEffectDestroy wccameffect;
	wccam cameraEffect ["terminate", "back"];
	camDestroy wccam;
	wccam = objNull;
	closeDialog 0;
	menuaction = -1;
};

waitUntil {isNull wccam};
wccam = "camera" camCreate [0, 0, 1000];
wccam cameraEffect ["internal", "back"];

showCinemaBorder false;
wccam camSetTarget clothes;
wccam camSetRelPos [-5, -10, 2.5];
wccam camCommit 0;
wccameffect = ppEffectCreate ["ColorCorrections", 1999];
wccameffect ppEffectEnable true;
wccameffect ppEffectAdjust [0.5, 0.7, 0.0, [1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 1.0]];
wccameffect ppEffectCommit 0;

disableSerialization;
_textbox = (uiNamespace getVariable "wcdisplay") displayCtrl 5001;

_array = [];

{
	_name = getText (configFile >> "CfgVehicles" >> _x >> "DisplayName");
	_index = lbAdd [5002, _name];
	_array set [_index, _x];
} foreach wcchangeclothes;

lbSetCurSel [5002, 0];

ctrlSetText [5005, "Change clothes"];

while {alive player && {dialog}} do {
	_index = lbCurSel 5002;
	_type = _array select _index;

	if (menuaction == 1) then {
		menuaction = -1;
		ppEffectDestroy wccameffect;
		wccam cameraEffect ["terminate", "back"];
		camDestroy wccam;
		wccam = objNull;
		closeDialog 0;

		[] call WC_fnc_saveloadout;

		if (_type in wcchangeclothescivil) then {
			_group = createGroup civilian;
		};

		if (_type in wcchangeclotheswest) then {
			_group = createGroup west;
		};

		if (_type in wcchangeclotheseast) then {
			_group = createGroup east;
		};

		_unit = _group createUnit [_type, getPosATL player, [], 0, "FORM"];
		{
			_unit disableAI _x;
		} foreach ["AUTOTARGET", "TARGET", "MOVE", "ANIM"];

		wcbackupbody = player;
		wcbackupposition = getPosATL player;

		_position = wcinitpos findEmptyPosition [1, 100];
		if (_position select 0 == 0) then {
			diag_log "WARCONTEXT: CANNOT FIND EMPTY POSITION FOR CHANGE CLOTHES BACKUP POSITION";
			_position = getMarkerPos "respawn_west";
		};
		wcbackupbody addEventHandler ["HandleDamage", {0}];
		wcbackupbody setPos _position;

		_unit addEventHandler ["killed", {
			wcgarbage = [(_this select 1)] spawn WC_fnc_restorebody;
			wcgarbage = [(_this select 0)] spawn WC_fnc_garbagecollector;

			switch (rank player) do {
				case "Private": {
					wcplayeraddscore = [player, -1];
				};

				case "Corporal": {
					wcplayeraddscore = [player, -2];
				};

				case "Sergeant": {
					wcplayeraddscore = [player, -3];
				};

				case "Lieutenant": {
					wcplayeraddscore = [player, -4];
				};

				case "Captain": {
					wcplayeraddscore = [player, -5];
				};

				case "Major": {
					wcplayeraddscore = [player, -7];
				};

				case "Colonel": {
					wcplayeraddscore = [player, -10];
				};
			};

			["wcplayeraddscore", "server"] call WC_fnc_publicvariable;

			wcaddscore = -1;
			["wcaddscore", "server"] call WC_fnc_publicvariable;
		}];

		selectPlayer _unit;

		// Leave the group of players
		_newgroup = createGroup west;
		[wcbackupbody] joinSilent _newgroup;

		wcgarbage = [] spawn WC_fnc_restoreactionmenu;

		wcgarbage = [] spawn WC_fnc_restoreloadout;

		(findDisplay 46) displayAddEventHandler ["KeyDown", "_this call WC_fnc_keymapper;"];

		if (count (units (group player)) > 0) then {
			_count = 0;
			{
				if (isPlayer _x) then {
					_count = _count + 1;
				};
			} foreach (units (group player));
			if (_count == 1) then {
				(group player) selectLeader player;
			};
		};
	};

	if (menuaction == 2) then {
		menuaction = -1;
		closeDialog 0;
	};

	sleep 0.5;
};

ppEffectDestroy wccameffect;
wccam cameraEffect ["terminate", "back"];
camDestroy wccam;
wccam = objNull;

wcgarbage = [player] spawn WC_fnc_stealth;